package soa.security.rs.exception;


import org.apache.cxf.message.Message;
import org.json.JSONObject;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

/**
 * @author CreGu
 * @date 2016/3/29
 */
public class SoaExceptionMapper implements ExceptionMapper {
    @Override
    public Response toResponse(Throwable exception) {
        JSONObject jsonObject = new JSONObject();
        boolean already = false;

        if(exception instanceof javax.ws.rs.InternalServerErrorException) {
            Throwable t = exception.getCause();
            jsonObject.put("success", false);
            jsonObject.put("message", t.getMessage());
        }else{
            jsonObject.put("success", false);
            jsonObject.put("message", exception.getMessage());
        }



        return Response.ok(jsonObject.toString()).encoding("UTF-8")
                .header(Message.CONTENT_TYPE, "application/json; charset=UTF-8")
                //.header("Access-Control-Allow-Origin","*")
                .build();
    }
}
